package com.eva.admin.api.cms;

import com.eva.admin.biz.cms.template.CmsTemplateBiz;
import com.eva.admin.biz.cms.template.dto.CreateCmsTemplateDTO;
import com.eva.admin.biz.cms.template.dto.QueryCmsTemplateDTO;
import com.eva.admin.biz.cms.template.dto.UpdateCmsTemplateDTO;
import com.eva.admin.biz.cms.template.vo.CmsTemplateVO;
import com.eva.dao.cms.model.CmsTemplate;
import com.eva.framework.common.api.BaseController;
import com.eva.framework.rbac.authorize.ContainPermissions;
import com.eva.framework.security.prevent.PreventRepeat;
import com.eva.framework.common.model.ApiResponse;
import com.eva.framework.common.model.PageData;
import com.eva.framework.common.model.PageWrap;
import com.eva.service.cms.CmsTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "模板管理")
@RestController
@RequestMapping("/cms/template")
public class CmsTemplateController extends BaseController {

    @Resource
    private CmsTemplateBiz cmsTemplateBiz;

    @Resource
    private CmsTemplateService cmsTemplateService;

    @PreventRepeat
    @ApiOperation("新建")
    @PostMapping("/create")
    @ContainPermissions("cms:template:create")
    public ApiResponse<?> create(@RequestBody CreateCmsTemplateDTO dto) {
        cmsTemplateBiz.create(dto);
        return ApiResponse.success(null);
    }

    @ApiOperation("删除")
    @GetMapping("/delete/{id}")
    @ContainPermissions("cms:template:delete")
    public ApiResponse<?> deleteById(@PathVariable Integer id) {
        cmsTemplateBiz.deleteById(id);
        return ApiResponse.success(null);
    }

    @ApiOperation("批量删除")
    @GetMapping("/delete/batch")
    @ContainPermissions("cms:template:delete")
    public ApiResponse<?> deleteByIdInBatch(@RequestParam String ids) {
        cmsTemplateBiz.deleteByIdInBatch(this.getIdList(ids));
        return ApiResponse.success(null);
    }

    @ApiOperation("修改")
    @PostMapping("/updateById")
    @ContainPermissions("cms:template:update")
    public ApiResponse<?> updateById(@RequestBody UpdateCmsTemplateDTO dto) {
        cmsTemplateBiz.updateById(dto);
        return ApiResponse.success(null);
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    @ContainPermissions("cms:template:query")
    public ApiResponse<PageData<CmsTemplateVO>> findPage (@RequestBody PageWrap<QueryCmsTemplateDTO> pageWrap) {
        return ApiResponse.success(cmsTemplateBiz.findPage(pageWrap));
    }

    @ApiOperation("查询所有")
    @GetMapping("/all")
    public ApiResponse<List<CmsTemplate>> findAll () {
        return ApiResponse.success(cmsTemplateService.findAll());
    }
}
